<?php namespace Admin\Controller;
//类型控制器
class TypeController extends CommonController{
	private $model;
	public function __auto(){
		$this->model = new \Common\Model\Type;
	}
	
	//类型列表
	public function index(){
		$data=$this->model->get();
		if(!$data){
			View::success('请先添加类型',U('add'));
		}
		
		$oldData=$data;
		View::with('oldData',$oldData);
		View::make();
	}
	
	//顶级类型添加
	public function add(){
		if(IS_POST){
			if($this->model->store()){
				View::success("添加成功",U('index'));
			}
			View::error($this->model->getError());
		}
		View::make();
	}
	
	//添加属性
	public function addValue(){
		if(IS_POST){
		    if($this->model->store()){
		    	 	View::success("添加成功",U('index'));
		    }
		   View::error($this->model->getError());
		}
		$tid=Q('get.tid',0,'intval');
		$type=$this->model->where("tid={$tid}")->find();
		View::with('type',$type);
		View::make();
	}
	
	//类型编辑
	public function edit(){
		if(IS_POST){
			if($this->model->edit()){
				View::success("修改成功",U('index'));
			}
			View::error($this->model->getError());
		}
		$tid=Q('get.tid',0,'intval');
		$oldData=$this->model->where("tid={$tid}")->find();
		View::with('oldData',$oldData);
		View::make();
	}
	
	//类型删除
	public function del(){
		$tid=Q('get.tid',0,'intval');
		$this->model->where("tid={$tid}")->delete();
		View::success("删除成功");
	}
}
